package com.lb.jwitter.action;


import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.util.Map; 
import org.springframework.test.context.ContextConfiguration; 
import com.lb.jwitter.service.TblQuyenChiTietService; 
import com.lb.jwitter.service.TblTaiKhoan; 
import com.opensymphony.xwork2.ActionContext; 
import java.util.Collection; 
import com.lb.jwitter.util.ApplicationContextProvider; 
import java.util.Date; 
import java.util.GregorianCalendar; 
import javax.xml.datatype.DatatypeFactory; 
import javax.xml.datatype.XMLGregorianCalendar; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import com.lb.jwitter.service.ConditionReport; 
import com.lb.jwitter.util.Message; 
import com.opensymphony.xwork2.Action; 
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblCTDTDMHocKyTrongNam;
import com.lb.jwitter.service.TblCTDTDMHocKyTrongNamService;

import com.lb.jwitter.service.TblCanBo;
import com.lb.jwitter.service.TblCanBoService;

import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;

 /** 
 * @author QUOCVIET  
 * 
 */ 


@ContextConfiguration(locations="classpath:beans.xml") 
public class TblDKMHDotDKUpdateAction{
    private int pageSize; 
    public int getPageSize() {
       return pageSize;
 }

    public void setPageSize(int pageSize) { this.pageSize = pageSize; }

    private int page; 
    public int getPage() { return page; }

    public void setPage(int page) { this.page = page; }

    private int pages; 
    public int getPages() { return pages; }

    public void setPages(int pages) { this.pages = pages; }

    private String dataString; 
    public String getDataString() { return dataString; }

    private Message msg; 
    public Message getMsg() { return msg; }

    private int id; 
    public int getId() {
       return id;
 }

    public void setId(int id) {
       this.id = id;
 }

    private String ma; 
    public void setMa(String ma) {
       this.ma = ma;
 }

    public String getMa() {
       return ma;
 }

    private String ten; 
    public void setTen(String ten) {
       this.ten = ten;
 }

    public String getTen() {
       return ten;
 }

    private String maLoai; 
    public void setMaLoai(String maLoai) {
       this.maLoai = maLoai;
 }

    public String getMaLoai() {
       return maLoai;
 }

    private int hocKyId; 
    public void setHocKyId(int hocKyId) {
       this.hocKyId = hocKyId;
 }

    public int getHocKyId() {
       return hocKyId;
 }

    private String ngayBatDau; 
    public void setNgayBatDau(String ngayBatDau) {
       this.ngayBatDau = ngayBatDau;
 }

    public String getNgayBatDau() {
       return ngayBatDau;
 }

    private String ngayKetThuc; 
    public void setNgayKetThuc(String ngayKetThuc) {
       this.ngayKetThuc = ngayKetThuc;
 }

    public String getNgayKetThuc() {
       return ngayKetThuc;
 }

    private String ngayCapNhat; 
    public void setNgayCapNhat(String ngayCapNhat) {
       this.ngayCapNhat = ngayCapNhat;
 }

    public String getNgayCapNhat() {
       return ngayCapNhat;
 }

    private String ghiChu; 
    public void setGhiChu(String ghiChu) {
       this.ghiChu = ghiChu;
 }

    public String getGhiChu() {
       return ghiChu;
 }

    private int trangThai; 
    public void setTrangThai(int trangThai) {
       this.trangThai = trangThai;
 }

    public int getTrangThai() {
       return trangThai;
 }

    private int canBoId; 
    public void setCanBoId(int canBoId) {
       this.canBoId = canBoId;
 }

    public int getCanBoId() {
       return canBoId;
 }

    private int dotDKTruocId; 
    public void setDotDKTruocId(int dotDKTruocId) {
       this.dotDKTruocId = dotDKTruocId;
 }

    public int getDotDKTruocId() {
       return dotDKTruocId;
 }

    private TblDKMHDotDK tblDKMHDotDK; 
    public void setTblDKMHDotDK (TblDKMHDotDK tblDKMHDotDK) {
        this.tblDKMHDotDK = tblDKMHDotDK;
 }

    private Collection<TblDKMHDotDK> tblDKMHDotDKs; 
    public void setTblDKMHDotDKs(Collection<TblDKMHDotDK> tblDKMHDotDKs) {
        this.tblDKMHDotDKs = tblDKMHDotDKs; 
      } 

    private TblDKMHDotDKService tblDKMHDotDKService; 

    private TblCTDTDMHocKyTrongNamService tblCTDTDMHocKyTrongNamService; 

    private TblCanBoService tblCanBoService; 


	private int preUpdate(Message msg, TblDKMHDotDK obj ){ 
        msg.setPreMessage_b(true); 
        msg.setPreMessage_s("success"); 
        return 1; 
    } 
	private int postUpdate(Message msg, TblDKMHDotDK obj ){ 
        msg.setPostMessage_b(true); 
        msg.setPostMessage_s("success"); 
        return 1; 
    } 
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
    public String execute() throws Exception 
   { 
    try{ 
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_DKMH_DotDK", "sua")){ 
        return "noprivilege"; 
    }
        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient"); 
        tblCTDTDMHocKyTrongNamService = (TblCTDTDMHocKyTrongNamService) context.getBean("tbl_CTDT_DM_HocKyTrongNamClient"); 

        tblCanBoService = (TblCanBoService) context.getBean("tbl_CanBoClient"); 

        tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient"); 

        msg = new Message(); 
        tblDKMHDotDK = tblDKMHDotDKService.gettblDKMHDotDK(new Long(id)); 
        tblDKMHDotDK.setMa(ma); 
        tblDKMHDotDK.setTen(ten); 
        tblDKMHDotDK.setMaLoai(maLoai); 
        tblDKMHDotDK.setHocKyId(tblCTDTDMHocKyTrongNamService.gettblCTDTDMHocKyTrongNam(new Long(hocKyId))); 
      
        
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); 
       Date dd = df.parse(ngayBatDau); 
       GregorianCalendar c = new GregorianCalendar(); 
       c.setTime(dd);
       XMLGregorianCalendar d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDK.setNgayBatDau(d); 
        
        dd = df.parse(ngayKetThuc); 
       c.setTime(dd);
        d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDK.setNgayKetThuc(d); 
        
       dd = new Date(); 
       c.setTime(dd);
       d = DatatypeFactory.newInstance().newXMLGregorianCalendar(c); 
        tblDKMHDotDK.setNgayCapNhat(d); 
        
        
        tblDKMHDotDK.setGhiChu(ghiChu); 
//        tblDKMHDotDK.setTrangThai(trangThai); 
        tblDKMHDotDK.setCanBoId(taiKhoanDangNhap.getCanBoId()); 
        tblDKMHDotDK.setDotDKTruocId(tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKTruocId))); 
        preUpdate(msg,tblDKMHDotDK); 
       if (msg.isPreMessage_b()) 
       { 
           tblDKMHDotDKService.updatetblDKMHDotDK(tblDKMHDotDK); 
           postUpdate(msg, tblDKMHDotDK); 
           if (msg.isPostMessage_b() == false) 
           { 
               return Action.ERROR; 
           } 
       }else{ 
           return Action.ERROR; 
       } 

        return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Update tbl_DKMH_DotDK Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 } 
 } 
 
